package com.wfk.plat.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wfk.entity.Customer;
import com.wfk.entity.LevelInfo;
import com.wfk.entity.MoneyDetails;
import com.wfk.enums.HandleType;
import com.wfk.plat.annotation.AuthResource;
import com.wfk.plat.mapping.MappingUtils;
import com.wfk.service.IAccountService;
import com.wfk.service.ICustomerService;
import com.wfk.service.ILevelInfoService;
import com.wfk.service.IMoneyDetailsService;
import com.wfk.utils.BasePage;
import com.wfk.utils.ResponseData;

@Controller
public class CustomerContorller {
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private ILevelInfoService levelInfoService;
	@Autowired
	private IMoneyDetailsService moneyDetailsService;
	@Autowired
	private IAccountService accountService;
	
	@AuthResource(url=MappingUtils.showCustomerList)
	@RequestMapping(value = MappingUtils.handleMoney)
	@ResponseBody
	public ResponseData handleMoney(Integer cid,Integer status,Integer typeid,BigDecimal money) {
		return this.accountService.handleMoney(cid, status,typeid, money);
	}
	
	@AuthResource(url=MappingUtils.showCustomerList)
	@RequestMapping(value = MappingUtils.customerOne)
	@ResponseBody
	public ResponseData customerOne(Integer cid) {
		return ResponseData.ok().putDataValue("data", this.customerService.getById(cid));
	}
	
	@AuthResource(url=MappingUtils.showCustomerList)
	@RequestMapping(value = MappingUtils.cusUpdateStatus)
	@ResponseBody
	public ResponseData cusUpdateStatus(Integer cid,Integer status) {
		Customer cus = this.customerService.getById(cid);
		if(cus==null) {
			return ResponseData.baseError("会员不存在");
		}else if(cus.getStatus()==status) {
			return ResponseData.baseError("已经是该状态");
		}
		cus.setStatus(status);
		boolean updateById = this.customerService.updateById(cus);
		return updateById?ResponseData.ok():ResponseData.baseError("操作失败");
	}
	
	
	/***
	 * 跳转会员列表页面
	 * @param isadd
	 * @param isedit
	 * @param isdelete
	 * @return
	 */
	@AuthResource
	@RequestMapping(value = MappingUtils.showCustomerList)
	public ModelAndView showCustomerList(Long loginId,Customer cus,BasePage<Customer> page) {
		ModelAndView view = new ModelAndView(MappingUtils.showCustomerList);
		List<LevelInfo> list = levelInfoService.list(null);
		this.customerService.listPage(page, cus);
		view.addObject("list",page);
		view.addObject("levelList", list);
		view.addObject("loginId", loginId);
		view.addObject("vo",cus);
		return view;
	}
	
	/***
	 * 会员金额操作
	 * @param isadd
	 * @param isedit
	 * @param isdelete
	 * @return
	 */
	@AuthResource(url=MappingUtils.showCustomerList)
	@RequestMapping(value = MappingUtils.moneyCost)
	public ModelAndView moneyCost(Long loginId,Integer cid) {
		ModelAndView view = new ModelAndView(MappingUtils.moneyCost);
		view.addObject("loginId", loginId);
		view.addObject("cid", cid);
		view.addObject("typeList",HandleType.queryList());
		return view;
	}
	
	
	/***
	 * 会员金额页面
	 * @param isadd
	 * @param isedit
	 * @param isdelete
	 * @return
	 */
	@AuthResource(url=MappingUtils.showCustomerList)
	@RequestMapping(value = MappingUtils.showMoneyDetail)
	public ModelAndView showMoneyDetails(Long loginId,MoneyDetails details,BasePage<MoneyDetails> page) {
		moneyDetailsService.listPage(details,page);
		ModelAndView view = new ModelAndView(MappingUtils.showMoneyDetail);
		view.addObject("vo", details);
		view.addObject("list", page);
		view.addObject("loginId", loginId);
		return view;
	}
}
